<?php
//Requires getIDByEmail, idIsValid, getResourceIDsInContainer
//isCurrentlyCheckedOut and connectDB
require("dbFunctions.php");

/**
 * Adds an entry to the Resource_Designation table. Checks out this resource and any
 * contained resources if they are currently available.
 * This file should be hidden from users using the web interface and is used to populate the database
 * from the mobile device specifically.
 *
 * Author: Steve Gennaoui
 * Date: 11/04/2012
 */

//Get the parameters
$resource_code = $_REQUEST['qr_code'];
$requestor_email = $_REQUEST['requestor_email'];
$supplier_email = $_REQUEST['supplier_email'];
$projected_checkin_unixtime = $_REQUEST['projected_checkin']; // Should be UNIX timestamp!
$auxillary_items = $_REQUEST['aux_items'];
$comments = $_REQUEST['comments'];

// Get IDs from emails
$requestor_id = getIDByEmail($requestor_email,"Requestors");
$supplier_id = getIDByEmail($supplier_email,"Suppliers");

// Get datetimes
$current_time = date("Y-m-d H:i:s");	//Gives the current datetime
$projected_checkin = date("Y-m-d H:i:s", $projected_checkin_unixtime);

// Verify all parameters are acceptable.
if( !idIsValid($resource_code,"QR_Code") )
	die("The QR_Code provided was not valid.");
if( isCurrentlyCheckedOut($resource_code))
	die("This resource is currently checked out.");
if( !isTopLevel($resource_code) && isPartOfPackage($resource_code) )
{	//QR Code is not top level, but is part of a package. Return details on the top level
	die("Resource is part of package. Top level item: \n".containerDetailsForResourceInPackage($resource_code)."\n This resource was not checked out.");
}
if( !idIsValid($requestor_id,"Requestors") )
	die("There was no Requestor found with the email $requestor_email");
if( !idIsValid($supplier_id,"Suppliers") )
	die("There was no Supplier found with the email $supplier_email");
if( is_null($current_time) )
	die("Server Side Error: Current time could not be created.");
if( is_null($projected_checkin) )
	die("The projected check in datetime could not be created from the provided checkin time.");

if( is_null($auxillary_items) )
	$auxillary_items = "";
if( is_null($comments) )
	$comments = "";

$items_to_be_checked_out = getResourceIDsInContainer($resource_code); //if this item is a container, get everything it contains.

//Verify that all resources are available to be checked out!
foreach($items_to_be_checked_out as $qr)
{
	//If there's a resource that is already checked out, we don't have to check anymore.
	//Die and report the offending resource
	if( isCurrentlyCheckedOut($qr) )
		die("Resource with QR Code $qr is currently checked out.");
}

// Get DB handle
$dbh = connectDB();

// If we get here, all items can be checked out. So do it!
foreach($items_to_be_checked_out as $qr)
{

	//Create query
	$query = "INSERT INTO Resource_Designation(qr_code,requestor_id,supplier_id_checkout,checkout_time,projected_checkin,auxillary_items,comments,supplier_id_checkin,has_been_checked_in) VALUES(:qr_code,:req_id,:sup_id_co,:co_time,:proj_ci,:aux_items,:comments,:sup_id_ci,:hbci)";

	$stmt = $dbh->prepare($query);
	$stmt->bindParam(':qr_code',$qr);
	$stmt->bindParam(':req_id',$requestor_id);
	$stmt->bindParam(':sup_id_co',$supplier_id);
	$stmt->bindParam(':co_time',$current_time);
	$stmt->bindParam(':proj_ci',$projected_checkin);
	$stmt->bindParam(':aux_items',$auxillary_items);
	$stmt->bindParam(':comments',$comments);
	$stmt->bindParam(':sup_id_ci',$supplier_id);
	$stmt->bindValue(':hbci',0);

	$stmt->execute();
}
//Release the db handle
$dbh = null;

?>
